//
//  YMSpecificationDetailVC.m
//  YMMedical
//
//  Created by y s on 2023/7/26.
//

#import "YMSpecificationDetailVC.h"
#import "YMWebBaseVC.h"
#import "YMNetwork.h"

@interface YMSpecificationDetailVC ()<UIDocumentPickerDelegate>

@property (nonatomic, strong) YMWebBaseVC *vcWeb;
@property (nonatomic, strong) UIView *vBg;
@property (nonatomic, strong) UIButton *btnDownload;


@end

@implementation YMSpecificationDetailVC


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}
#pragma mark —- UI methods
- (void)setUI{
    [self showNavTitle:[SDataUtil stringOf:_strName valid:KLanguage(@"operate_book_txt")]];
    [self.view addSubview:self.vBg];
    [self addChildViewController:self.vcWeb];
    [self.vBg addSubview:self.vcWeb.view];
    [self.view addSubview:self.btnDownload];
    
    [self.vBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([SFitUtil fit:15]);
        make.left.mas_equalTo([SFitUtil fit:10]);
        make.right.mas_equalTo([SFitUtil fit:-10]);
    }];
    [self.btnDownload mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.vBg.mas_bottom).offset([SFitUtil fit:12]);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo([SFitUtil fitSize:345 height:50]);
        make.bottom.mas_equalTo(-IPHONE_X_BOTTOM_HEIGHT-[SFitUtil fit:15]);
    }];
    [self.vcWeb.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    self.vcWeb.webUrl = self.strUrl;
    [self.vcWeb loadData];
}

- (void)downloadClickAction:(id)sender{
    WEAK_SELF;
    NSString *pathUrl = self.strUrl;
    [SHUD showScreenWait:@""];
    [YMNetwork downloadWithUrl:pathUrl success:^(NSURL * _Nonnull path) {
        [SHUD hide];
        [weakSelf showDocumentPicker:path];
    } failure:^(NSError * _Nonnull error) {
        [SHUD hide];
        [SHUD showInfo:KLanguage(@"down_fail_txt")];
    }];
}

- (void)showDocumentPicker:(NSURL *)pathUrl{
    if (pathUrl == nil){
        return;
    }
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithURL:pathUrl inMode:UIDocumentPickerModeExportToService];
    documentPicker.delegate = self;
    documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:documentPicker animated:YES completion:nil];
}

#pragma mark - UIDocumentInteractionControllerDelegate
-(UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller {
    return self;
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller {
    return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller {
    return self.view.frame;
}
#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls {
     //保存成功
    [SHUD showInfo:KLanguage(@"save_success_txt")];
}
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller{
     //取消保存
}


- (UIView *)vBg{
    if (_vBg == nil){
        _vBg = [[UIView alloc] init];
        _vBg.backgroundColor = [UIColor clearColor];
//        _vBg.backgroundColor = STheme.D9;
        _vBg.layer.cornerRadius = [SFitUtil fit:8];
        _vBg.clipsToBounds = YES;
    }
    return _vBg;
}
- (YMWebBaseVC *)vcWeb{
    if (_vcWeb == nil){
        _vcWeb = [[YMWebBaseVC alloc] init];
        _vcWeb.view.backgroundColor = [UIColor clearColor];
    }
    return _vcWeb;
}
- (UIButton *)btnDownload{
    if (_btnDownload == nil){
        _btnDownload = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnDownload setTitle:KLanguage(@"down_file_txt") forState:UIControlStateNormal];
        [_btnDownload setTitleColor:STheme.D9 forState:UIControlStateNormal];
        _btnDownload.titleLabel.font = [SFitUtil fitBoldFont:17];
        [_btnDownload addTarget:self action:@selector(downloadClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnDownload.backgroundColor = STheme.D12;
        _btnDownload.layer.cornerRadius = [SFitUtil fit:10];
        _btnDownload.clipsToBounds = YES;
    }
    return _btnDownload;
}



@end
